$(function() {
    $(".header_top_left .logo").hover(function() {
        console.log(666);
        $(this).css("color", "red").siblings().css("color", "")
    }, function() {
        $(this).css("color", "")
    })

    $(".item").hover(function() {
        console.log(66);
        $(this).css("color", "black").siblings().css("color", "")
    }, function() {
        $(this).css("color", "")
    })

})


//谁登陆的 就看谁的购物车
let userid = window.localStorage.getItem("user");
console.log(userid);
if (userid) {
    //获取用户的数据
    $.ajax({
            type: "get",
            url: "../php/showCart.php?user=" + userid,
            dataType: "json"
        }).then(data => {
                console.log(666);
                console.log(data);

                let list = data.list
                console.log(list);
                //list是集合  item 是对象  map会返回也给新的数组
                $(".tbody").html(list.map(item => `<div class="tr">
             <div class="item">
             <input type="checkbox" class="checkone" data-id=${item.goodsid} />
             </div>
             <div class="item">
                 <img src=${item.goodsimg}>
             </div>
             <div class="item fize">
                 ${item.goodsname}
             </div>
             <div class="item color_">
                 ${"￥"+(item.goodsprice * 1).toFixed(2)}
             </div>
             <div class="item">
             ${item.buynum > 1 ? `<button class=reduce data-id=${item.userid} data-price=${item.goodsprice} data-goodsid=${item.goodsid}>-</button>` : `<button class=reduce data-id=${item.userid} data-price=${item.goodsprice} data-goodsid=${item.goodsid} style='display:none'>-</button>`}
             <input value=${item.buynum} class=buynum />
                 
        <button class=add data-id=${item.userid} data-price=${item.goodsprice} data-goodsid=${item.goodsid}>+</button>

             </div>
             <div class="item subprice" >
                 ${item.goodsprice * item.buynum}
             </div>
             <div class="item del" data-id=${item.goodsid} >
                 删除
             </div>
             
         </div>`).join(""))
     })




 } else {
     window.location.href = "login.html";
 }


 //单删
 $(document).on("click", ".tbody .del", function () {
     let id = $(this).attr("data-id");
     
     console.log(id);
     $.ajax({
         type: "get",
         url: "../php/delete.php",
         data: {
             id
         },
         dataType: "json"

     }).then(res => {
         console.log(res);
         if (res.status = true) {

             $(this).parent().remove();
             getTotal();
         } else {
             alert("更新失败");
         }
     })
 })



 //全选反选
 $(document).on("click", ".checkall", function () {
     let flag = $(this).prop("checked");
     $(".checkone").prop("checked", flag);
     getTotal();
 })


 $(document).on("click", ".checkone", function () {
     let flag = $(".checkone:checked").length == $(".checkone").length;
     $(".checkall").prop("checked", flag);
     getTotal();
 })




 //加法
 $(document).on("click", ".tbody .add", function () {
   
     $(this).prop("disabled", true);
     console.log(this);
     $.ajax({
         type: "get",
         url: "../php/updateshoppingcar.php",
         data: {
            uid: $(this).attr("data-id"),
            goodsid: $(this).attr("data-goodsid"),
             num: 1
         },
         
         dataType: "json"

     }).then(res => {
         if (res.status) {
             
             $(this).prop("disabled", false);
             let numInput = $(this).prev();
             
             let reduceBtn = numInput.prev();
             let totalSpan = $(this).parent().next();
             let goodsprice = $(this).attr("data-price");

             numInput.val(numInput.val() * 1 + 1);
             totalSpan.html((numInput.val() * goodsprice).toFixed(2));
             reduceBtn.show();
             getTotal();
         }
     })
     
     //1:前面的数量+1
     //2：后面的小计也更新
     //3：有加 减号肯定有
     //应该是数据库的数据更新了 页面才变化
 })

 //减法

 $(document).on("click", ".tbody .reduce", function () {
    
     $(this).prop("disabled", true);
     console.log(this);
     $.ajax({
         type: "get",
         url: "../php/updateshoppingcar.php",
         data: {
             uid: $(this).attr("data-id"),
             goodsid: $(this).attr("data-goodsid"),
             num: -1
         },
         dataType: "json"
     }).then(res => {
         if (res.status) {
             //请求完成
             $(this).prop("disabled", false);
             let numInput = $(this).next();
             let totalSpan = $(this).parent().next();
             let goodsprice = $(this).attr("data-price");
             if (numInput.val() == 1) {
                 return false;
             }
             numInput.val(numInput.val() * 1 - 1);
             totalSpan.html((numInput.val() * goodsprice).toFixed(2));
             if (numInput.val() == 1) {
                 $(this).hide();
             }
             getTotal();

         }
     })
 })





 $(document).on("click", ".delall", function () {//批量删除
    console.log(666);
    //获取所有选中的id
    let ids = $(".checkone:checked").map((index, item) => {     
   return $(item).attr("data-id");

    }).get();
console.log(ids);
    $.ajax({
        type: "get",
        url: "../php/delete.php",
        data: {
            id: ids.join(",")
        },
        dataType: "json"
    }).then(res => {
        console.log(res);
        if (res.code == 1) {
            //删除成功;
            $(".checkone:checked").each((index, item) => {
                $(item).parent().parent().remove();
            })
            getTotal();
        }

    })




})




 



 function getTotal() {
     let num = 0;
     let price = 0;

     if ($(".checkone:checked").length) {
         num = $(".checkone:checked").map((_index, item) => {
             let tr = $(item).parent().parent();
             return tr.find(".buynum").val() * 1;
         }).get().reduce((a, b) => a + b);

         price = $(".checkone:checked").map((_index, item) => {
             let tr = $(item).parent().parent();
             return tr.find(".subprice").html() * 1;
         }).get().reduce((a, b) => a + b);

     }


     $("#totalnum").html(num);
     $("#totalprice").html(price);



 }